package com.csu.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.tour.entity.User;
import com.csu.tour.mapper.UserMapper;
import com.csu.tour.service.UserService;
import com.csu.tour.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.List;

@Component
public class UserServiceImpl  extends ServiceImpl<UserMapper,User> implements UserService {
     @Autowired
     private UserMapper userMapper;



    @Override
    public UserVO getUserByNameAndPassword(String username, String password) {
        UserVO userVO = new UserVO();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        queryWrapper.eq("password", password);
        List<User> users = this.list(queryWrapper);
        if (users != null ) {
            User user = users.getFirst();
            userVO.setUsername(username);
            userVO.setPassword(password);
            userVO.setAge(user.getAge());
            userVO.setSex(user.getSex());
            userVO.setId(user.getId());
            userVO.setPhone(user.getPhone());
            userVO.setRole(user.getRole());
            return userVO;
        }else{
            return null;
        }
    }

    @Override
    public UserVO getUserByNamePasswordRole(String username, String password, String role) {
        UserVO userVO = new UserVO();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        queryWrapper.eq("password", password);
        queryWrapper.eq("role", role);
        List<User> users = this.list(queryWrapper);
//         && !users.isEmpty()
        if (users != null) {
            User user = users.getFirst();
            userVO.setUsername(username);
            userVO.setPassword(password);
            userVO.setAge(user.getAge());
            userVO.setSex(user.getSex());
            userVO.setId(user.getId());
            userVO.setRole(user.getRole());
            userVO.setPhone(user.getPhone());
            return userVO;
        }else{
            return null;
        }
    }

    @Override
    public void addUser(UserVO userVO) {
        User user = new User();
        user.setUsername(userVO.getUsername());
        user.setPassword(userVO.getPassword());
        user.setAge(userVO.getAge());
        user.setSex(userVO.getSex());
        user.setId(userVO.getId());
        user.setPhone(userVO.getPhone());
        user.setRole(userVO.getRole());

        userMapper.insert(user);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public List<User> selectTourists(String role) {
        return userMapper.selectList(new QueryWrapper<User>().eq("role",role ));
    }


}
